package www.xudan.android_ffmpeg.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @auther XuDan on 2020/3/18.
 */
public class FileUtil {
    public static void copyAssetsToSD(Context context, String asstes_name, String copy_path) {
        Log.i("xd----", copy_path);
        AssetManager assets = context.getAssets();
        InputStream is = null;
        FileOutputStream fos = null;
        File out_file = new File(copy_path);

        try {
            is = assets.open(asstes_name);
            if (!out_file.getParentFile().exists()) {
                out_file.getParentFile().mkdirs();
            }
            if (!out_file.exists()) {
                out_file.createNewFile();
            }
            fos = new FileOutputStream(out_file);
            byte[] buf = new byte[1024];
            while (true) {
                int read = is.read(buf);
                if (read == -1) {
                    return;
                }
                fos.write(buf);
                fos.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }

                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
